package com.island.controller.user;

import com.alibaba.fastjson.JSON;
import com.island.pojo.Bill;
import com.island.pojo.Provider;
import com.island.pojo.Role;
import com.island.pojo.User;
import com.island.service.role.RoleService;
import com.island.service.user.UserService;
import com.island.utils.PageSupport;
import com.island.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/page")
public class UserPageController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PageSupport pageSupport;

    @RequestMapping("/updatePwdPage")   //修改密码页面跳转
    public String updatePwdPage(){
        return "pwdmodify";
    }

    @RequestMapping("/userManager")
    public String nextPageUserList(String queryname,String queryUserRole, String pageIndex, HttpServletRequest req){
        int UserRole = 0;   //设置

        //System.out.println(queryname+queryUserRole);
        if (queryUserRole!=null && queryUserRole!=""){
            UserRole = Integer.parseInt(queryUserRole);
        }

        //3、根据usename或userRole获取相应表中数据的总数
        int totalCount = userService.getUserCount(queryname,UserRole);
        //4、根据当前页数和相应表中数据的总数通过PageUtil获取相应的pageSupport对象
        pageSupport=PageUtil.getPageSupport(pageIndex,totalCount);

        //System.out.println(pageSupport.getCurrentPageNo());
        //5、用户列表展示
        List<User> userList = userService.getUserList(queryname,UserRole,(pageSupport.getCurrentPageNo()-1)* pageSupport.getPageSize(), pageSupport.getPageSize());
        //将集合返回给前端进行解析显示
        req.setAttribute("userList",userList);

        //6、角色列表展示
        List<Role> roleList = roleService.getRoleList();
        req.setAttribute("roleList",roleList);

        //7、将参数返回前端
        req.setAttribute("queryUserName",queryname);
        req.setAttribute("queryUserRole",queryUserRole);
        req.setAttribute("pageSupport",pageSupport);
        return "userlist";
    }

    @RequestMapping("/viewuser")
    public String viewUser(String uid, Model model){

        int userId = Integer.parseInt(uid);

        User user = userService.queryUserById(userId);
        model.addAttribute("user",user);
        return "userview";
    }

    @RequestMapping("/tomodifyuser")
    public String toModifyUser(String uid,Model model){
        int uId = Integer.parseInt(uid);
        User user = userService.queryUserById(uId);
        System.out.println(user);
        model.addAttribute("user",user);
        return "usermodify";
    }

    @RequestMapping("/modifyuser")
    public String modifyUser(HttpServletRequest request) throws ParseException {

        HashMap<String, Object> map = new HashMap<String, Object>();
        String uid = request.getParameter("uid");
        map.put("userId",Integer.valueOf(uid));
        map.put("userName",request.getParameter("userName"));
        map.put("gender",Integer.valueOf(request.getParameter("gender")));
        SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        map.put("birthday",format.parse(request.getParameter("birthday")));
        map.put("phone",request.getParameter("phone"));
        map.put("address",request.getParameter("address"));
        map.put("userRole",Integer.valueOf(request.getParameter("userRole")));
        map.put("modifyBy",Integer.valueOf(request.getParameter("modifyBy")));
        map.put("modifyDate",new Date());
        userService.updateUserById(map);

        return "redirect:/page/userManager";
    }

    @RequestMapping("/adduser")
    public String addUser(HttpServletRequest request) throws ParseException {

        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("userCode",request.getParameter("userCode"));
        map.put("userName",request.getParameter("userName"));
        map.put("userPassword",request.getParameter("userPassword"));
//        解析gender
        String gender = request.getParameter("gender");
        System.out.println(request.getParameter("gender"));
        map.put("gender",Integer.valueOf(gender));
//        解析birthday
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = format.parse((String) request.getParameter("birthday"));
        map.put("birthday",birthday);
        map.put("phone",request.getParameter("phone"));
        map.put("address",request.getParameter("address"));
        map.put("userRole",request.getParameter("userRole"));
        map.put("createdBy",Integer.valueOf(request.getParameter("createdBy")));
        map.put("creationDate",new Date());
        userService.addUser(map);
        return "redirect:/page/userManager";
    }

    @RequestMapping("/deleteuser")
    @ResponseBody
    public String deleteUser(String uid){
        HashMap<String,String> map = new HashMap<String, String>();
        int userId = Integer.parseInt(uid);
        User user = userService.queryUserById(userId);
        if(user!=null){
            try {
               userService.deleteUserById(userId);
                System.out.println("您成功删除了"+user);
                map.put("delResult","true");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("您没能删除"+user);
                map.put("delResult","false");
            }
        }else {
            System.out.println("该用户不存在");
            map.put("deResulte","notexist");
        }
        return JSON.toJSONString(map);

    }

    @RequestMapping("/getRoleList")
    @ResponseBody
    public String getRoleList(){
        List<Role> roles = roleService.getRoleList();
//        for (Role role : roles) {
//            System.out.println(role);
//        }
        return JSON.toJSONString(roles);
    }

    @RequestMapping("/userifexist")
    @ResponseBody
    public String UserIfExist(String userCode){
        Map<String,String> map = new HashMap<String, String>();

        User user = userService.queryUserByUserCode(userCode);
        if (user!=null){
            map.put("userCode","exist");
        }
        return JSON.toJSONString(map);
    }
}
